package com.polaris.live.adapter

import android.widget.ImageView
import com.blankj.utilcode.util.ColorUtils
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.common.constant.AppConst
import com.polaris.live.databinding.AdapterBeautyBinding
import com.polaris.live.resp.back_resp.BeautyItem
import com.polaris.live.utils.zego.beauty.BeautyFilterEnum

class BeautyAdapter : BaseBindingAdapter<AdapterBeautyBinding, BeautyItem>() {

    private var selectPosition = 0

    override fun convert(holder: VBViewHolder<AdapterBeautyBinding>, item: BeautyItem) {
        holder.vb.apply {
            val position = holder.absoluteAdapterPosition

            beautyItemText.text = item.name
            setImage(item, beautyItemImage)
            if (position == selectPosition) {
                beautyItemText.setTextColor(ColorUtils.getColor(R.color.white))
                beautyItemBack.setBackgroundResource(R.drawable.r8_back_ffffff_5_with_2_fe1849)
            } else {
                beautyItemText.setTextColor(ColorUtils.getColor(R.color.ffffff_50))
                beautyItemBack.setBackgroundResource(R.drawable.r8_back_ffffff_5)
            }
        }
    }

    private fun setImage(item: BeautyItem, beautyItemImage: ImageView) {
        val drawableRes = when (item.type) {
            //美颜
            AppConst.Beautify.MEIBAI -> R.drawable.meibai
            AppConst.Beautify.MOPI -> R.drawable.mopi
            AppConst.Beautify.QINGXI -> R.drawable.qingxi
            AppConst.Beautify.BAIYA -> R.drawable.baiya
            AppConst.Beautify.LIANGYAN -> R.drawable.liangyan
            //美型
            AppConst.DeformType.SHOULIAN -> R.drawable.shoulian
            AppConst.DeformType.DAYAN -> R.drawable.dayan
            AppConst.DeformType.YANJINGWEIZHI -> R.drawable.yanjingweizhi
            AppConst.DeformType.FAJIXIAN -> R.drawable.fajixian
            AppConst.DeformType.XIABA -> R.drawable.xiaba
            AppConst.DeformType.SHOUBI -> R.drawable.shoubi
            AppConst.DeformType.BICHANG -> R.drawable.bichang
            AppConst.DeformType.ZUIXING -> R.drawable.zuixing
            else -> {
                BeautyFilterEnum.getFilterByType(item.type).iconId
            }
        }

        beautyItemImage.setImageResource(drawableRes)
    }

    fun setSelect(position: Int) {
        selectPosition = position
        notifyDataSetChanged()
    }

    fun clearSelected() {
        if (selectPosition == -1) {
            return
        }
        val index = selectPosition
        selectPosition = -1
        notifyItemChanged(index)
    }
}